package com.bys.managercolok;

import android.app.Activity;
import android.app.Dialog;
import android.app.Service;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.security.SecureRandom;

public class ClockAlarmActivity  extends Activity {
    private  MediaPlayer mediaPlayer;
    private  Vibrator vibrator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock_alarm);
//        提示信息
        String msg = this.getIntent().getStringExtra("msg");
//        闹钟响铃的方式
        int flag = this.getIntent().getIntExtra("flag", 0);
        showDialogInBroadcastReceiver(msg,flag);
    }

    /**
     * 广播显示的对话框
     * @param msg
     * @param flag
     */
    private void showDialogInBroadcastReceiver(String msg, int flag) {
        if(flag == 1 || flag == 2){
             mediaPlayer = MediaPlayer.create(this, R.raw.alert_wake);
//            设置是否循环播放
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }
        if(flag == 0 || flag == 2) {
//             获取振动器 实例
            vibrator  = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);
//            指定手机以pattern指定的模式振动!
//            第一个参数为等待指定时间后开始震动，震动时间为第二个参数。后边的参数依次为等待震动和震动的时间
//            第二个参数为重复次数，-1为不重复，0为一直震动
            vibrator.vibrate(new long[]{100,120,100,600},0);
        }
        SimpleDialog simpleDialog = new SimpleDialog(this, R.style.Theme_dialog);
        simpleDialog.setTitle("闹钟提醒");
        simpleDialog.setMessage(msg);
        if(!this.isFinishing())
            simpleDialog.show();
        simpleDialog.setClickListener( v ->{
            if(simpleDialog.btn_confirm == v ){
                if(flag == 1 || flag == 2){
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                if(flag == 0 || flag == 2){
                    vibrator.cancel();
                }
                simpleDialog.dismiss();
                finish();
            }

        });


    }
}
